<?php

use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
use Fisharebest\Webtrees\Http\RequestHandlers\SiteLogsData;
use Fisharebest\Webtrees\Http\RequestHandlers\SiteLogsDelete;
use Fisharebest\Webtrees\Http\RequestHandlers\SiteLogsDownload;
use Fisharebest\Webtrees\Http\RequestHandlers\SiteLogsPage;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\View;

?>

<?= view('components/breadcrumbs', ['links' => [route(ControlPanel::class) => I18N::translate('Control panel'), $title]]) ?>

<h1><?= $title ?></h1>

<form method="post" class="form" name="logs">
    <?= csrf_field() ?>

    <div class="row">
        <div class="form-group col-sm-6 col-sm-3">
            <label for="from">
                <?= I18N::translateContext('Start of date range', 'From') ?>
            </label>
            <input type="date" class="form-control" id="from" max="<?= e($latest) ?>" min="<?= e($earliest) ?>" name="from" value="<?= e($from) ?>" required>
        </div>

        <div class="form-group col-sm-6 col-sm-3">
            <label for="to">
                <?= I18N::translateContext('End of date range', 'To') ?>
            </label>
            <input type="date" class="form-control" id="to" max="<?= e($latest) ?>" min="<?= e($earliest) ?>" name="to" value="<?= e($to) ?>" required>
        </div>

        <div class="form-group col-sm-6 col-sm-2">
            <label for="type">
                <?= I18N::translate('Type') ?>
            </label>
            <?= view('components/select', ['name' => 'type', 'selected' => $type, 'options' => ['' => '', 'auth' => 'auth', 'config' => 'config', 'debug' => 'debug', 'edit' => 'edit', 'error' => 'error', 'media' => 'media', 'search' => 'search']]) ?>
        </div>

        <div class="form-group col-sm-6 col-sm-4">
            <label for="ip">
                <?= I18N::translate('IP address') ?>
            </label>
            <input class="form-control" type="text" id="ip" name="ip" value="<?= e($ip) ?>">
        </div>
    </div>

    <div class="row">
        <div class="form-group col-sm-4">
            <label for="text">
                <?= I18N::translate('Message') ?>
            </label>
            <input class="form-control" type="text" id="text" name="text" value="<?= e($text) ?>">
        </div>

        <div class="form-group col-sm-4">
            <label for="username">
                <?= I18N::translate('User') ?>
            </label>
            <?= view('components/select', ['name' => 'username', 'selected' => $username, 'options' => $user_options]) ?>
        </div>

        <div class="form-group col-sm-4">
            <label for="tree">
                <?= I18N::translate('Family tree') ?>
            </label>
            <?= view('components/select', ['name' => 'tree', 'selected' => $tree, 'options' => $tree_options]) ?>
        </div>
    </div>

    <div class="text-center">
        <button type="submit" class="btn btn-primary">
        <?= view('icons/search') ?>
            <?= /* I18N: A button label. */ I18N::translate('search') ?>
        </button>

        <a href="<?= e(route(SiteLogsPage::class)) ?>" type="submit" class="btn btn-secondary">
            <?= view('icons/cancel') ?>
            <?= /* I18N: A button label. */ I18N::translate('reset') ?>
        </a>

        <a href="<?= e(route(SiteLogsDownload::class, ['from' => $from, 'to' => $to, 'type' => $type, 'text' => $text, 'ip' => $ip, 'username' => $username, 'tree' => $tree])) ?>" class="btn btn-secondary">
        <?= view('icons/download') ?>
            <?= /* I18N: A button label. */ I18N::translate('download') ?>
        </a>

        <a href="#" class="btn btn-danger" data-confirm="<?= I18N::translate('Permanently delete these records?') ?>" data-post-url="<?= e(route(SiteLogsDelete::class, ['from' => $from, 'to' => $to, 'type' => $type, 'text' => $text, 'ip' => $ip, 'username' => $username, 'tree' => $tree])) ?>">
        <?= view('icons/delete') ?>
            <?= /* I18N: A button label. */ I18N::translate('delete') ?>
        </a>
    </div>
</form>

<table
    class="table table-bordered table-sm table-hover table-site-logs"
    <?= view('lists/datatables-attributes') ?>
    data-ajax="<?= e(route(SiteLogsData::class, ['from' => $from, 'to' => $to, 'type' => $type, 'text' => $text, 'ip' => $ip, 'username' => $username, 'tree' => $tree])) ?>"
    data-server-side="true"
    data-filter="false"
>
    <thead>
        <tr>
            <th></th>
            <th><?= I18N::translate('Timestamp') ?></th>
            <th><?= I18N::translate('Type') ?></th>
            <th><?= I18N::translate('Message') ?></th>
            <th><?= I18N::translate('IP address') ?></th>
            <th><?= I18N::translate('User') ?></th>
            <th><?= I18N::translate('Family tree') ?></th>
        </tr>
    </thead>
</table>

<?php View::push('javascript') ?>
<script>
  $(".table-site-logs").dataTable( {
    processing: true,
    sorting: [[ 0, "desc" ]],
    columns: [
      /* log_id      */ { visible: false },
      /* Timestamp   */ { sort: 0 },
      /* Type        */ { },
      /* message     */ { },
      /* IP address  */ { },
      /* User        */ { },
      /* Family tree */ { }
    ]
  });
</script>
<?php View::endpush() ?>
